/**
 * 
 */
package com.supcon.tinyportal.tags;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import com.supcon.tinyportal.util.RequestUtil;
import com.supcon.tinyportal.util.TagUtil;

/**
 * @author michael
 * 
 */
public class ReloadTag extends BodyTagSupport {
	private static final long serialVersionUID = 1L;
	private boolean redirect = true;

	public boolean isRedirect() {
		return redirect;
	}

	public void setRedirect(boolean redirect) {
		this.redirect = redirect;
	}

	private String target;

	public String getTarget() {
		return target;
	}

	public void setTarget(String target) {
		this.target = target;
	}

	public int doStartTag() throws JspException {
		HttpServletRequest request = (HttpServletRequest) (pageContext.getRequest());
		if (!RequestUtil.isAjax(request))
			RequestUtil.updatePortletInfo(pageContext);
		return EVAL_BODY_INCLUDE;
	}

	public int doAfterBody() throws JspException {
		if (bodyContent != null) {
			String value = bodyContent.getString().trim();
			if (value.length() > 0)
				target = value;
		}
		return (SKIP_BODY);
	}

	public int doEndTag() throws JspException {
		return TagUtil.reload(pageContext, target, redirect);
	}
}
